# load packages

pacman::p_load(tidyverse, sf, geosphere, lwgeom, tmap)

### figure A.9(a) (locations of matched precincts: berlin) ----

# load data
berlin_bezirk_borders <- read_rds("data/berlin_bezirk_borders.rds")
berlin_pools <- read_rds("data/berlin_pools.rds")
berlin_matched <- read_rds("data/berlin_matched.rds")


# create map
berlin_urnen_map <- tm_shape(berlin_matched) +
    tm_borders(lwd=0.5, alpha = 0.5) +
    tm_fill("treated",
            pal = c("#5f7cbf", "#162b96"),
            colorNA = "#ece7f2",
            textNA = "Excluded",
            title = "Matching") +
    tm_shape(berlin_bezirk_borders) +
    tm_borders(col = "black") +
    tm_shape(berlin_pools) +
    tm_dots(size = 0.2, col = c("#d7191c")) +
    tm_legend(show = T, frame = T, bg.color = "white", bg.alpha = 0.9, 
              frame.lwd = .5, legend.position = c(.8,.85), legend.outside = F
    ) +
    tm_layout(frame = F)

berlin_urnen_map

# clean
rm(list=ls())

### figure A.9(b) (locations of matched precincts: hamburg) ----

# load data
hamburg_bezirk_borders <- read_rds("data/hamburg_bezirk_borders.rds")
hamburg_pools <- read_rds("data/hamburg_pools.rds")
hamburg <- read_rds("data/hamburg_matched.rds")

hamburg_urnen_map <- tm_shape(hamburg) +
    tm_borders(lwd=0.5, alpha = 0.5) +
    tm_fill("treated",
            pal = c("#5f7cbf", "#162b96"),
            colorNA = "#ece7f2",
            textNA = "Excluded",
            title = "Matching") +
    tm_shape(hamburg_bezirk_borders) +
    tm_borders(col = "black") +
    tm_shape(hamburg_pools) +
    tm_dots(size = 0.2, col = c("#d7191c")) +
    tm_legend(show = T, frame = T, bg.color = "white", bg.alpha = 0.9, 
              frame.lwd = .5, legend.position = c(.8,.85), legend.outside = F
    ) +
    tm_layout(frame = F)

hamburg_urnen_map

# clean
rm(list = ls())
